﻿using System.Windows.Forms;
using BobDotNet.Classes;
using BobDotNet.Classes.Dao;
using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using BobDotNet.Classes.Application;

namespace BobDotNet.Forms.Tools
{
    public partial class Connexion : Form
    {
        private bool retour;
        public User sUser;

        public Connexion()
        {
            InitializeComponent();
        }

        public bool Montre()
        {
            txtLogin.Text = Environment.UserName;
            this.ShowDialog();
            return retour;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            User u = DaoUser.Charge(txtLogin.Text, txtMdp.Text);

            if (u.Id.Equals(0))
            {
                retour = false;
                MessageBox.Show("Mauvais utilisateur et/ou mot de passe");
            }
            else
            {
                if (u.DateFinValidite != null)
                {
                    if (u.DateFinValidite < DateTime.Now)
                    {
                        retour = false;
                        MessageBox.Show("L'utilisateur est Obsoléte/Inactif.");
                        return;
                    }
                }
            }

            CallContext.SetData(KEY.USER_CONNECTED_KEY, u);

            retour = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            retour = false;
            this.Close();
        }
    }
}
